﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using WeSAddress.model;
using WeSLog.util;
using System.Data;

namespace WeSAddress
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class WeSAddress : IWeSAddress
    {

        public List<Address> convertDataTableToList(DataTable dt)
        {
                List<Address> listAddress = new List<Address>();

            foreach (DataRow dr in dt.Rows)
            {
                Address address = new Address();
                address.streetName = dr["StreetName"].ToString();
                address.zipCode = Convert.ToInt32(dr["ZipCode"]); 
                address.city = dr["City"].ToString();
                listAddress.Add(address);
            }
            return listAddress;
        }

        public List<Address> convertDataTableToListHouseNumber(DataTable dt)
        {
            List<Address> listAddress = new List<Address>();

            foreach (DataRow dr in dt.Rows)
            {
                Address address = new Address();
                address.houseNumber = Convert.ToInt32(dr["HouseNr"]);
                address.houseChar = dr["HouseChr"].ToString();
                listAddress.Add(address);
            }
            return listAddress;
        }

        public WeSAddressResult getAddress(string address)
        {
            if (address != "" && address !=null)
            {
                try
                {
                    DataTable result=null;
                    List<Address> end=null;
                     result= DB.searchAddress(address);
                      end= convertDataTableToList(result);
                    if(end.Count!=0)
                    {
                    return new WeSAddressResult
                    {
                        address= end,
                        weSAddressStatusResult = new WeSAddressStatusResult
                        {
                            statusCode = WeSAddressStatusResult.STATUS_VALID_TOKEN_CODE,
                            statusText = WeSAddressStatusResult.STATUS_VALID_TOKEN_TEXT
                        }
                    };
                    }
                    else
                    {
                        return new WeSAddressResult
                        {
                            weSAddressStatusResult = new WeSAddressStatusResult
                            {
                                statusCode = WeSAddressStatusResult.STATUS_INVALID_ADDRESS_CODE,
                                statusText = WeSAddressStatusResult.STATUS_INVALID_ADDRESS_TEXT
                            }
                        };
                    }
                }
                catch (Exception ex)
                {
                    return new WeSAddressResult
                    {
                        weSAddressStatusResult = new WeSAddressStatusResult
                        {
                            statusCode = WeSAddressStatusResult.STATUS_INVALID_ADDRESS_CODE,
                            statusText = WeSAddressStatusResult.STATUS_INVALID_ADDRESS_TEXT
                        }
                    };
                }
            }
            else
            {
                return new WeSAddressResult
                {
                    weSAddressStatusResult = new WeSAddressStatusResult
                    {
                        statusCode = WeSAddressStatusResult.STATUS_INVALID_ADDRESS_CODE,
                        statusText = WeSAddressStatusResult.STATUS_INVALID_ADDRESS_TEXT
                    }
                };
            }
        }
        
      //--------------


        public WeSAddressResult getHouseNumber(int houseNumber, string streetName, string zipcode, string city)
        {
            if (houseNumber!=0)
            {
                try
                {
                    DataTable result = null;
                    List<Address> houseNumberResult = null;
                    result = DB.getHouseNumber(houseNumber,streetName,zipcode,city);
                    houseNumberResult = convertDataTableToListHouseNumber(result);
                    if (houseNumberResult.Count!=0)
                    {
                        return new WeSAddressResult
                        {
                            address=houseNumberResult,
                            weSAddressStatusResult = new WeSAddressStatusResult
                            {
                                statusCode = WeSAddressStatusResult.STATUS_VALID_TOKEN_CODE,
                                statusText = WeSAddressStatusResult.STATUS_VALID_TOKEN_TEXT
                            },
                            //result = string.Format("With ZipCode = " + houseNumber + " the City is :" + city)
                        };
                    }
                    else
                    {
                        return new WeSAddressResult
                        {
                            weSAddressStatusResult = new WeSAddressStatusResult
                            {
                                statusCode = WeSAddressStatusResult.STATUS_INVALID_HOUSENUMBER_CODE,
                                statusText = WeSAddressStatusResult.STATUS_INVALID_HOUSENUMBER_TEXT
                            }
                        };
                    }
                }
                catch (Exception ex)
                {
                    return new WeSAddressResult
                    {
                        weSAddressStatusResult = new WeSAddressStatusResult
                        {
                            statusCode = WeSAddressStatusResult.STATUS_INVALID_HOUSENUMBER_CODE,
                            statusText = WeSAddressStatusResult.STATUS_INVALID_HOUSENUMBER_TEXT
                        }
                    };
                }
            }
            else
            {
                return new WeSAddressResult
                {

                    weSAddressStatusResult = new WeSAddressStatusResult
                    {
                        statusCode = WeSAddressStatusResult.STATUS_ZERO_ZIPCODE_CODE,
                        statusText = WeSAddressStatusResult.STATUS_ZERO_ZIPCODE_TEXT
                    }
                };
            }
        }

        ///-------------
        public WeSAddressResult getCity(int zipcode)
        {
            if (zipcode != 0)
            {
                try
                {
                    string city = "";
                    city = DB.getValue("select City from [GEOADR] where ZipCode = '" + zipcode + "'");
                    if (city != "")
                    {
                        return new WeSAddressResult
                        {
                            weSAddressStatusResult = new WeSAddressStatusResult
                            {
                                statusCode = WeSAddressStatusResult.STATUS_VALID_TOKEN_CODE,
                                statusText = WeSAddressStatusResult.STATUS_VALID_TOKEN_TEXT
                            },
                            result = string.Format("With ZipCode = " + zipcode + " the City is :" + city)
                        };
                    }
                    else
                    {
                        return new WeSAddressResult
                        {
                            weSAddressStatusResult = new WeSAddressStatusResult
                            {
                                statusCode = WeSAddressStatusResult.STATUS_INVALID_ZIPCODE_CODE,
                                statusText = WeSAddressStatusResult.STATUS_INVALID_ZIPCODE_TEXT
                            }
                        };
                    }
                }
                catch (Exception ex)
                {
                    return new WeSAddressResult
                    {
                        weSAddressStatusResult = new WeSAddressStatusResult
                        {
                            statusCode = WeSAddressStatusResult.STATUS_INVALID_ZIPCODE_CODE,
                            statusText = WeSAddressStatusResult.STATUS_INVALID_ZIPCODE_TEXT
                        }
                    };
                }
            }
            else
            {
                return new WeSAddressResult
                {
                    
                    weSAddressStatusResult = new WeSAddressStatusResult
                    {
                        statusCode = WeSAddressStatusResult.STATUS_ZERO_ZIPCODE_CODE,
                        statusText = WeSAddressStatusResult.STATUS_ZERO_ZIPCODE_TEXT
                    }
                };
            }
        }

    }
}
